// Copyright (c) 2023 Institute of Software, Chinese Academy of Sciences
// Ripple is licensed under Mulan PSL v2.
// You can use this software according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of Mulan PSL v2 at:
//          http://license.coscl.org.cn/MulanPSL2
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
// See the Mulan PSL v2 for more details.

package ripple.common.tcp.encoder;

import io.netty.buffer.ByteBuf;
import ripple.common.entity.Constants;
import ripple.common.entity.NodeMetadata;
import ripple.common.entity.Reform;
import ripple.common.helper.TypeHelper;
import ripple.common.tcp.Encoder;
import ripple.common.tcp.Message;
import ripple.common.tcp.message.MQSyncRequest;
import ripple.common.tcp.message.ReformSyncRequest;

public class ReformSyncRequestEncoder implements Encoder {
    @Override
    public void encode(Message message, ByteBuf byteBuf) {
        ReformSyncRequest reformSyncRequest = (ReformSyncRequest) message;
        byteBuf.writeByte(reformSyncRequest.getType().getValue());
        TypeHelper.writeUuid(reformSyncRequest.getUuid(), byteBuf);
        TypeHelper.writeUuid(reformSyncRequest.getMessageUuid(), byteBuf);
        TypeHelper.writeString(reformSyncRequest.getOperationType(), byteBuf);
        if (reformSyncRequest.getOperationType().equals(Constants.REFORM_COMMIT)) {
            TypeHelper.writeString(NodeMetadata.ListToString(reformSyncRequest.getNodeList()), byteBuf);
//        } else if (MQSyncRequest.getOperationType().equals(Constants.MESSAGE_TYPE_INCREMENTAL_UPDATE)) {
//            TypeHelper.writeUuid(MQSyncRequest.getBaseMessageUuid(), byteBuf);
//            TypeHelper.writeString(MQSyncRequest.getAtomicOperation(), byteBuf);
//            TypeHelper.writeString(MQSyncRequest.getValue(), byteBuf);
        }
        byteBuf.writeLong(reformSyncRequest.getLastUpdate().getTime());
        byteBuf.writeInt(reformSyncRequest.getLastUpdateServerId());
    }
}